package com.yoda.mall.admin.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.CouponApi;
import com.yoda.mall.api.CouponProductCategoryRelationApi;
import com.yoda.mall.api.CouponProductRelationApi;
import com.yoda.mall.model.dto.CouponDto;
import com.yoda.mall.model.entity.sms.Coupon;
import com.yoda.mall.model.entity.sms.CouponProductCategoryRelation;
import com.yoda.mall.model.entity.sms.CouponProductRelation;
import com.yoda.mall.model.vo.CouponVo;
import com.yoda.mall.model.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CouponService {
    @DubboReference
    private CouponApi couponApi;
    @DubboReference
    private CouponProductCategoryRelationApi couponProductCategoryRelationApi;
    @DubboReference
    private CouponProductRelationApi couponProductRelationApi;

    public void createCoupon(CouponDto couponDto) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponDto, coupon);
        couponApi.createCoupon(coupon);
        for (CouponProductCategoryRelation productCategoryRelation : couponDto.getProductCategoryRelationList()) {
            couponProductCategoryRelationApi.addCouponProductCategoryRelation(productCategoryRelation);
        }
        couponDto.getProductRelationList().forEach(couponProductRelation -> couponProductRelationApi.addCouponProductRelation(couponProductRelation));
    }

    public void deleteCoupon(Long id) {
        couponApi.deleteCouponById(id);
        couponProductCategoryRelationApi.deleteCouponProductCategoryRelationByCouponId(id);
        couponProductRelationApi.deleteCouponProductRelationByCouponId(id);
    }

    public PageResult<Coupon> couponListPage(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<Coupon> page = couponApi.getCouponListPage(name, pageNum, pageSize, type);
        return new PageResult<>((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), page.getRecords());
    }

    public void updateCoupon(CouponDto couponDto) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponDto, coupon);
        couponApi.updateCoupon(coupon);
        couponProductCategoryRelationApi.deleteCouponProductCategoryRelationByCouponId(coupon.getId());
        couponProductRelationApi.deleteCouponProductRelationByCouponId(coupon.getId());
        for (CouponProductCategoryRelation productCategoryRelation : couponDto.getProductCategoryRelationList()) {
            couponProductCategoryRelationApi.addCouponProductCategoryRelation(productCategoryRelation);
        }
        couponDto.getProductRelationList().forEach(couponProductRelation -> couponProductRelationApi.addCouponProductRelation(couponProductRelation));
    }

    public CouponVo getCouponInfo(Long id) {
        Coupon coupon = couponApi.getCouponById(id);
        List<CouponProductCategoryRelation> couponProductCategoryRelations = couponProductCategoryRelationApi.getCouponProductCategoryRelationListByCouponId(id);
        List<CouponProductRelation> couponProductRelations = couponProductRelationApi.getCouponProductRelationListByCouponId(id);
        return CouponVo.init(coupon, couponProductCategoryRelations, couponProductRelations);
    }
}
